package com.smart.interceptor;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class WeChatAuthorityInterceptor implements HandlerInterceptor{
    static Map<String,String> canPass=new HashMap<>();
    static
    {
    	canPass.put("/wechat/wx","");
    	canPass.put("/views/wechat/authorize.html","");
    	canPass.put("/wechat/web_authority","");
    	canPass.put("/wechat/rand_redirect", "");
    }
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		System.out.println("WeChatAuthorityInterceptor preHandle method start.....");
		String uri=request.getRequestURI();
		HttpSession session=request.getSession();
		System.out.println("当前请求的url："+uri);
		String Webroot=request.getContextPath();
		uri=uri.substring(Webroot.length());
		if(canPass.containsKey(uri))
		{
			System.out.println(uri+",为提供给微信服务器接入或者授权引导页面或者验证码的访问，允许访问");
			return true;
		}
		//说明没有进行网页授权，或者网页授权已经失效，那么就要引导其进行授权
		else if(session.getAttribute("openId")==null)
		{
			System.out.println(uri+",没有授权，阻止访问");
			response.sendRedirect("/SmartCommunity/views/wechat/authorize.html");
			return false;
		}
		else
		{
		    System.out.println(uri+",有授权");
		    System.out.println("sessionId:"+session.getId());
		    System.out.println("openId:"+session.getAttribute("openId"));
		    int verified=(Integer)session.getAttribute("verified");
		    System.out.println("verified:"+verified);
		    System.out.println(uri+",进行权限判断");
		    if(uri.equals("/views/wechat/register.html"))
		    {
		    	System.out.println("认证页面");
		    	if(verified==0) 
		    	{
		    		System.out.println("由于还没认证，跳到认证页面");
		    		return true;
		    	}
		    	else 
		    	{
		    		System.out.println("由于已经认证，跳到认证成功页面");
		    		response.sendRedirect("/SmartCommunity/views/wechat/register-success.html");
		    		return false;
		    	}
		    }
		    else if(uri.equals("/views/wechat/no-authority.html")||uri.equals("/wechat/get_community_list")||uri.equals("/views/wechat/home.html")||uri.equals("/wechat/get_randCode")||uri.equals("/wechat/reg"))
		    {
		    	System.out.println("不需要权限判断，就可以访问");
		    	return true;
		    }
		    else
		    {
		      if(verified==1) 
			  {
				System.out.println("已认证用户，允许访问");
			    return true;
			  }
			  else
			  {
				System.out.println("未认证用户，不允许访问");
				response.sendRedirect("/SmartCommunity/views/wechat/no-authority.html");
				return false;
			  }
		    }
		}
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
	}
   
}
